package com.srs.action.group;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.srs.action.BaseAction;
import com.srs.dao.GroupDao;
import com.srs.dao.RGroupRoleDao;
import com.srs.dao.RGroupScopeDao;
import com.srs.dao.UserDao;
import com.srs.dao.cfg.AreaDao;
import com.srs.dao.cfg.CountryDao;
import com.srs.dao.cfg.ProvinceDao;
import com.srs.dao.cfg.RoleDao;
import com.srs.dao.cfg.ScopeDao;
import com.srs.model.Group;
import com.srs.model.RGroupRole;
import com.srs.model.RGroupScope;
import com.srs.model.cfg.Role;
import com.srs.model.cfg.Scope;

public class GroupAction extends BaseAction {

	private Integer page;
	private Integer pageNum;	
	private Integer start;
	private Integer end;
	private Integer total;	
	
	private Group group;
	private List<Group> groups;
	
	private String roles;
	private String scopes;
	private String scopeItems;
	private Integer countRole;
	private Integer countScope;
	private Integer numOfRole;
	private Integer numOfScope;
	
	private List<RGroupRole> rGroupRoles;
	private List<RGroupScope> rGroupScopes;
	
	private String json;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Action group/list - Group management
	 * 
	 * @return
	 */
	public String list() {
		System.out.println("GroupAction::list");
		GroupDao groupDao = new GroupDao();
		groups = groupDao.list();
		
		if (page==null){
			page = 1;
		}
		if (groups.size()%RECORD_PER_PAGE==0){
			pageNum = groups.size()/RECORD_PER_PAGE;
		} else {
			pageNum = groups.size()/RECORD_PER_PAGE + 1;
		}
		
		start = (page-1)*RECORD_PER_PAGE + 1;
		total = groups.size();
		if (total<(page)*RECORD_PER_PAGE){
			end = total;
		} else {
			end = (page)*RECORD_PER_PAGE;
		}
		
		List<Group> pageGroups = new ArrayList<Group>() ;
		for(int i=0; i<groups.size(); i++){
			if (i>=(page-1)*RECORD_PER_PAGE && i<(page)*RECORD_PER_PAGE){
				groups.get(i).setIndex(i+1);
				pageGroups.add(groups.get(i));
			}
		}
		groups = pageGroups;		
		return SUCCESS;
	}

	/**
	 * Action group/view - Group management
	 * 
	 * @return
	 */
	public String view() {
		System.out.println("GroupAction::view");
		GroupDao groupDao = new GroupDao();
		group = groupDao.get(group.getGroupId());
		return SUCCESS;
	}

	/**
	 * Action group/add - Group management
	 * 
	 * @return
	 */
	public String add() {
		System.out.println("GroupAction::add");
		RoleDao roleDao	= new RoleDao();
		ScopeDao scopeDao = new ScopeDao();
		setNumOfRole(roleDao.list().size());
		setNumOfScope(scopeDao.list().size());
		return SUCCESS;
	}

	/**
	 * Action group/addComplete - Group management
	 * 
	 * @return
	 */
	public String addComplete() {
		System.out.println("GroupAction::addComplete");
		GroupDao groupDao = new GroupDao();
		RGroupRoleDao rGroupRoleDao = new RGroupRoleDao();
		RGroupScopeDao rGroupScopeDao = new RGroupScopeDao();
		groupDao.insert(group);
		group = groupDao.get(group.getGroupId()+1 );
		
		// Add all group role and group scope
		String[] scopeArray = scopes.split(",");
		String[] roleArray = roles.split(",");
		String[] scopeItemArray = scopeItems.split(",");
		for(int i=0; i< roleArray.length; i++){
			RGroupRole rGroupRole = new RGroupRole();
			rGroupRole.setGroup(group);
			try {
				rGroupRole.setRoleId(Integer.parseInt(roleArray[i]));
				rGroupRoleDao.insert(rGroupRole);				
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}

		}
		for(int i=0; i< scopeArray.length; i++){
			RGroupScope rGroupScope = new RGroupScope();
			rGroupScope.setGroup(group);
			try {
				rGroupScope.setScopeId(Integer.parseInt(scopeArray[i]));
				rGroupScope.setScopeValueId(Integer.parseInt(scopeItemArray[i]));
				rGroupScopeDao.insert(rGroupScope);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
				
		return SUCCESS;
	}

	/**
	 * Action group/edit - Group management
	 * 
	 * @return
	 */
	public String edit() {
		System.out.println("GroupAction::edit");
		GroupDao groupDao = new GroupDao();
		RGroupRoleDao rGroupRoleDao = new RGroupRoleDao();
		RGroupScopeDao rGroupScopeDao = new RGroupScopeDao();
		ScopeDao scopeDao = new ScopeDao();
		RoleDao roleDao = new RoleDao();
		
		group = groupDao.get(group.getGroupId());
		rGroupRoles = rGroupRoleDao.list(group);
		rGroupScopes = rGroupScopeDao.list(group);
		countRole = rGroupRoles.size()+1;
		countScope = rGroupScopes.size()+1;
		setNumOfRole(roleDao.list().size());
		setNumOfScope(scopeDao.list().size());
		for(int i=0; i<rGroupRoles.size(); i++){
			rGroupRoles.get(i).setIndex(i+1);
			Role role = roleDao.get(rGroupRoles.get(i).getRoleId());
			rGroupRoles.get(i).setRoleName(role.getRoleName());
		}
		CountryDao countryDao = new CountryDao();
		AreaDao areaDao = new AreaDao();
		ProvinceDao provinceDao = new ProvinceDao();
		for(int i=0; i<rGroupScopes.size(); i++){
			rGroupScopes.get(i).setIndex(i+1);
			Scope scope = scopeDao.get(rGroupScopes.get(i).getScopeId());
			rGroupScopes.get(i).setScopeName(scope.getScopeName());
			if (scope.getScopeName().equalsIgnoreCase("SCOPE_COUNTRY")){
				try {
					rGroupScopes.get(i).setScopeValueName(countryDao.get(rGroupScopes.get(i).getScopeValueId()).getCountryName());
				} catch (Exception e) {
					rGroupScopes.get(i).setScopeValueName("Không tìm thấy");
				}
			} else if (scope.getScopeName().equalsIgnoreCase("SCOPE_AREA")){
				try {
					rGroupScopes.get(i).setScopeValueName(areaDao.get(rGroupScopes.get(i).getScopeValueId()).getAreaName());
				} catch (Exception e) {
					rGroupScopes.get(i).setScopeValueName("Không tìm thấy");
				}
			} else if (scope.getScopeName().equalsIgnoreCase("SCOPE_PROVINCE")){
				try {
					rGroupScopes.get(i).setScopeValueName(provinceDao.get(rGroupScopes.get(i).getScopeValueId()).getProvinceName());
				} catch (Exception e) {
					// TODO: handle exception
				}
			}						
		}		
		return SUCCESS;
	}

	/**
	 * Action group/editComplete - Group management
	 * 
	 * @return
	 */
	public String editComplete() {
		System.out.println("GroupAction::editComplete");
		GroupDao groupDao = new GroupDao();
		RGroupRoleDao rGroupRoleDao = new RGroupRoleDao();
		RGroupScopeDao rGroupScopeDao = new RGroupScopeDao();
		
		groupDao.update(group);

		// Remove all group role and group scope
		rGroupRoleDao.deleteByGroupId(group.getGroupId());
		rGroupScopeDao.deleteByGroupId(group.getGroupId());
		
		// Add all group role and group scope
		String[] scopeArray = scopes.split(",");
		String[] roleArray = roles.split(",");
		String[] scopeItemArray = scopeItems.split(",");
		for(int i=0; i< roleArray.length; i++){
			try {
				RGroupRole rGroupRole = new RGroupRole();
				rGroupRole.setGroup(group);
				rGroupRole.setRoleId(Integer.parseInt(roleArray[i]));
				rGroupRoleDao.insert(rGroupRole);
			} catch (Exception e) {
			}
		}
		for(int i=0; i< scopeArray.length; i++){
			try {
				RGroupScope rGroupScope = new RGroupScope();
				rGroupScope.setGroup(group);
				rGroupScope.setScopeId(Integer.parseInt(scopeArray[i]));
				rGroupScope.setScopeValueId(Integer.parseInt(scopeItemArray[i]));
				rGroupScopeDao.insert(rGroupScope);
				
			} catch (Exception e) {
				// TODO: handle exception
			}
		}		
		
		return SUCCESS;
	}

	/**
	 * Action group/deleteComplete - Group management
	 * 
	 */
	public String deleteComplete() {
		System.out.println("GroupAction::deleteComplete");
		
		RGroupRoleDao groupRoleDao = new RGroupRoleDao();
		groupRoleDao.deleteByGroupId(group.getGroupId());
		
		RGroupScopeDao groupScopeDao = new RGroupScopeDao();
		groupScopeDao.deleteByGroupId(group.getGroupId());
		
		GroupDao groupDao = new GroupDao();
		groupDao.delete(group.getGroupId());
		return SUCCESS;
	}
	
	/**
	 * Action group/listJson - Role management
	 * 
	 */
	public String nameIsUsed() {
		System.out.println("RoleAction::listJson");

		// Init Dao
		GroupDao groupDao = new GroupDao();
		
		// Put to json;
		setJson("{nameIsUsed: " +
				JSONArray.fromObject(groupDao.nameIsUsed(group.getGroupName())).toString() +
				"}");
		return SUCCESS;
	}		
	
	/**
	 * Action group/listJson - Role management
	 * 
	 */
	public String groupIsUsed() {
		System.out.println("RoleAction::groupIsUsed");

		// Init Dao
		UserDao userDao = new UserDao();
		
		// Put to json;
		setJson("{groupIsUsed: " +
				JSONArray.fromObject(userDao.groupIsUsed(group.getGroupId())).toString() +
				"}");
		return SUCCESS;
	}		

	/**
	 * Action group/listJson - Role management
	 * 
	 */
	public String listJson() {
		System.out.println("RoleAction::listJson");

		// Init Dao
		GroupDao groupDao = new GroupDao();
		
		// Put to json;
		setJson("{groups: " +
				JSONArray.fromObject(groupDao.list()).toString() +
				"}");
		return SUCCESS;
	}	
	
	/*********************************************************************************************/
	
	/**
	 * @return the page
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page the page to set
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return the pageNum
	 */
	public Integer getPageNum() {
		return pageNum;
	}

	/**
	 * @param pageNum the pageNum to set
	 */
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	/**
	 * @return the start
	 */
	public Integer getStart() {
		return start;
	}

	/**
	 * @param start the start to set
	 */
	public void setStart(Integer start) {
		this.start = start;
	}

	/**
	 * @return the end
	 */
	public Integer getEnd() {
		return end;
	}

	/**
	 * @param end the end to set
	 */
	public void setEnd(Integer end) {
		this.end = end;
	}

	/**
	 * @return the total
	 */
	public Integer getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * @return the group
	 */
	public Group getGroup() {
		return group;
	}

	/**
	 * @param group the group to set
	 */
	public void setGroup(Group group) {
		this.group = group;
	}

	/**
	 * @return the groups
	 */
	public List<Group> getGroups() {
		return groups;
	}

	/**
	 * @param groups the groups to set
	 */
	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	/**
	 * @return the rscope
	 */
	public String getRoles() {
		return roles;
	}

	/**
	 * @param roles the roles to set
	 */
	public void setRoles(String roles) {
		this.roles = roles;
	}

	/**
	 * @return the scopes
	 */
	public String getScopes() {
		return scopes;
	}

	/**
	 * @param scopes the scopes to set
	 */
	public void setScopes(String scopes) {
		this.scopes = scopes;
	}

	/**
	 * @return the scopeItems
	 */
	public String getScopeItems() {
		return scopeItems;
	}

	/**
	 * @param scopeItems the scopeItems to set
	 */
	public void setScopeItems(String scopeItems) {
		this.scopeItems = scopeItems;
	}

	/**
	 * @return the rGroupRoles
	 */
	public List<RGroupRole> getrGroupRoles() {
		return rGroupRoles;
	}

	/**
	 * @param rGroupRoles the rGroupRoles to set
	 */
	public void setrGroupRoles(List<RGroupRole> rGroupRoles) {
		this.rGroupRoles = rGroupRoles;
	}



	/**
	 * @return the rGroupScopes
	 */
	public List<RGroupScope> getrGroupScopes() {
		return rGroupScopes;
	}

	/**
	 * @param rGroupScopes the rGroupScopes to set
	 */
	public void setrGroupScopes(List<RGroupScope> rGroupScopes) {
		this.rGroupScopes = rGroupScopes;
	}

	/**
	 * @return the countRole
	 */
	public Integer getCountRole() {
		return countRole;
	}

	/**
	 * @param countRole the countRole to set
	 */
	public void setCountRole(Integer countRole) {
		this.countRole = countRole;
	}

	/**
	 * @return the countScope
	 */
	public Integer getCountScope() {
		return countScope;
	}

	/**
	 * @param countScope the countScope to set
	 */
	public void setCountScope(Integer countScope) {
		this.countScope = countScope;
	}

	/**
	 * @return the json
	 */
	public String getJson() {
		return json;
	}

	/**
	 * @param json the json to set
	 */
	public void setJson(String json) {
		this.json = json;
	}

	/**
	 * @return the numOfRole
	 */
	public Integer getNumOfRole() {
		return numOfRole;
	}

	/**
	 * @param numOfRole the numOfRole to set
	 */
	public void setNumOfRole(Integer numOfRole) {
		this.numOfRole = numOfRole;
	}

	/**
	 * @return the numOfScope
	 */
	public Integer getNumOfScope() {
		return numOfScope;
	}

	/**
	 * @param numOfScope the numOfScope to set
	 */
	public void setNumOfScope(Integer numOfScope) {
		this.numOfScope = numOfScope;
	}
	
}
